package com.mall.service.commodity.controller;

import com.mall.commons.common.CommonPage;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.PmsProductAttributeCategory;
import com.mall.model.vo.PmsProductAttributeCategoryVo;
import com.mall.service.commodity.service.PmsProductAttributeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeCategoryController {
    @Autowired
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    //添加商品属性分类
    @PostMapping("/category/create")
    public CommonResult create(@RequestParam("name") String name) {
        pmsProductAttributeCategoryService.create(name);
        return CommonResult.success(1, "新增成功");
    }

    //删除单个商品属性分类
    @GetMapping("/category/delete/{id}")
    public CommonResult deleteById(@PathVariable Long id) {
        pmsProductAttributeCategoryService.deleteById(id);
        return CommonResult.success(1, "删除成功");
    }

    //分页获取所有商品属性分类
    @GetMapping("/category/list")
    public CommonResult<CommonPage<PmsProductAttributeCategory>> seleteAll(Integer pageNum, Integer pageSize) {
        CommonPage<PmsProductAttributeCategory> page =
                pmsProductAttributeCategoryService.seleteAll(pageNum, pageSize);
        return CommonResult.success(page);
    }

    //获取所有商品属性分类及其下属性
    @GetMapping("/category/list/withAttr")
    public CommonResult<List<PmsProductAttributeCategoryVo>> listWithAttr() {
        List<PmsProductAttributeCategoryVo> list = pmsProductAttributeCategoryService.listWithAttr();
        return CommonResult.success(list);
    }

    //修改商品属性分类
    @PostMapping("/category/update/{id}")
    public CommonResult updateById(@PathVariable Long id, @RequestBody String name) {
        pmsProductAttributeCategoryService.updateByCategoryId(id, name);
        return CommonResult.success(1, "修改成功");
    }

    //获取单个商品属性分类信息
    @GetMapping("category/{id}")
    public CommonResult<PmsProductAttributeCategory> seleteById(@PathVariable Long id) {
        PmsProductAttributeCategory pmsProductAttributeCategory = pmsProductAttributeCategoryService.seleteById(id);
        return CommonResult.success(pmsProductAttributeCategory);
    }
}
